package ThroughputDelay.NIO;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class ClientThrDelNIO {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        for (int j = 0; j < ServerThrDelNIO.POOL_SIZE; j++) {
            new Thread(() -> {
                try (SocketChannel sChannel = SocketChannel.open()) {

                    sChannel.configureBlocking(true);
                    sChannel.connect(new InetSocketAddress("localhost", 7004));
                    ObjectInputStream is = new ObjectInputStream(sChannel.socket().getInputStream());
                    DataOutputStream os = new DataOutputStream(sChannel.socket().getOutputStream());

                    switch (ServerThrDelNIO.mode){
                        case THROUGHPUT:
                            for (int i = 0; i < ServerThrDelNIO.MESSAGE_COUNT; i++) {
                                is.readObject();
                            }
                            break;
                        case DELAY:
                            for (int i = 0; i < ServerThrDelNIO.MESSAGE_COUNT; i++) {
                                os.writeUTF("GET");
                                is.readObject();
                                os.writeUTF("CONFIRM");
                            }
                            os.writeUTF("STOP");
                            break;
                    }
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
